<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="小区" prop="community">
      <el-input v-model="dataForm.community" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="房型" prop="roomType">
      <el-input v-model="dataForm.roomType" placeholder="房型"></el-input>
    </el-form-item>
    <el-form-item label="户型" prop="houseType">
      <el-input v-model="dataForm.houseType" placeholder="户型"></el-input>
    </el-form-item>
    <el-form-item label="朝向" prop="oriented">
      <el-input v-model="dataForm.oriented" placeholder="朝向"></el-input>
    </el-form-item>
    <el-form-item label="装修" prop="decoration">
      <el-input v-model="dataForm.decoration" placeholder="装修"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="floor">
      <el-input v-model="dataForm.floor" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="images">
      <el-input v-model="dataForm.images" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="配套" prop="matching">
      <el-input v-model="dataForm.matching" placeholder="配套"></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="label">
      <el-input v-model="dataForm.label" placeholder="标签"></el-input>
    </el-form-item>
    <el-form-item label="文本" prop="text">
      <el-input v-model="dataForm.text" placeholder="文本"></el-input>
    </el-form-item>
    <el-form-item label="归属人" prop="ownerId">
      <el-input v-model="dataForm.ownerId" placeholder="归属人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="挂牌价格" prop="listingPrice">
      <el-input v-model="dataForm.listingPrice" placeholder="挂牌价格"></el-input>
    </el-form-item>
    <el-form-item label="租售状态" prop="rentalStatus">
      <el-input v-model="dataForm.rentalStatus" placeholder="租售状态"></el-input>
    </el-form-item>
    <el-form-item label="有钥匙" prop="isKey">
      <el-input v-model="dataForm.isKey" placeholder="有钥匙"></el-input>
    </el-form-item>
    <el-form-item label="合同开始日期" prop="contractStartDate">
      <el-input v-model="dataForm.contractStartDate" placeholder="合同开始日期"></el-input>
    </el-form-item>
    <el-form-item label="合同结束日期" prop="contractEndDate">
      <el-input v-model="dataForm.contractEndDate" placeholder="合同结束日期"></el-input>
    </el-form-item>
    <el-form-item label="客户id" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="客户id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          address: '',
          community: '',
          roomType: '',
          houseType: '',
          oriented: '',
          decoration: '',
          floor: '',
          images: '',
          matching: '',
          label: '',
          text: '',
          ownerId: '',
          mobile: '',
          area: '',
          listingPrice: '',
          rentalStatus: '',
          isKey: '',
          contractStartDate: '',
          contractEndDate: '',
          customerId: ''
        },
        dataRule: {
          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          community: [
            { required: true, message: '小区不能为空', trigger: 'blur' }
          ],
          roomType: [
            { required: true, message: '房型不能为空', trigger: 'blur' }
          ],
          houseType: [
            { required: true, message: '户型不能为空', trigger: 'blur' }
          ],
          oriented: [
            { required: true, message: '朝向不能为空', trigger: 'blur' }
          ],
          decoration: [
            { required: true, message: '装修不能为空', trigger: 'blur' }
          ],
          floor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          matching: [
            { required: true, message: '配套不能为空', trigger: 'blur' }
          ],
          label: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ],
          text: [
            { required: true, message: '文本不能为空', trigger: 'blur' }
          ],
          // ownerId: [
          //   { required: true, message: '归属人不能为空', trigger: 'blur' }
          // ],
          mobile: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          listingPrice: [
            { required: true, message: '挂牌价格不能为空', trigger: 'blur' }
          ],
          rentalStatus: [
            { required: true, message: '租售状态不能为空', trigger: 'blur' }
          ],
          isKey: [
            { required: true, message: '有钥匙不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.address = data.house.address
                this.dataForm.community = data.house.community
                this.dataForm.roomType = data.house.roomType
                this.dataForm.houseType = data.house.houseType
                this.dataForm.oriented = data.house.oriented
                this.dataForm.decoration = data.house.decoration
                this.dataForm.floor = data.house.floor
                this.dataForm.images = data.house.images
                this.dataForm.matching = data.house.matching
                this.dataForm.label = data.house.label
                this.dataForm.text = data.house.text
                this.dataForm.ownerId = data.house.ownerId
                this.dataForm.mobile = data.house.mobile
                this.dataForm.area = data.house.area
                this.dataForm.listingPrice = data.house.listingPrice
                this.dataForm.rentalStatus = data.house.rentalStatus
                this.dataForm.isKey = data.house.isKey
                this.dataForm.contractStartDate = data.house.contractStartDate
                this.dataForm.contractEndDate = data.house.contractEndDate
                this.dataForm.customerId = data.house.customerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'address': this.dataForm.address,
                'community': this.dataForm.community,
                'roomType': this.dataForm.roomType,
                'houseType': this.dataForm.houseType,
                'oriented': this.dataForm.oriented,
                'decoration': this.dataForm.decoration,
                'floor': this.dataForm.floor,
                'images': this.dataForm.images,
                'matching': this.dataForm.matching,
                'label': this.dataForm.label,
                'text': this.dataForm.text,
                'ownerId': this.getUserId(),
                'mobile': this.dataForm.mobile,
                'area': this.dataForm.area,
                'listingPrice': this.dataForm.listingPrice,
                'rentalStatus': this.dataForm.rentalStatus,
                'isKey': this.dataForm.isKey,
                'contractStartDate': this.dataForm.contractStartDate,
                'contractEndDate': this.dataForm.contractEndDate,
                'customerId': this.dataForm.customerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
